﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;

public class NewBehaviourCloud : MonoBehaviour
{
    public float offset=0.1f;
    public int stackCount=8;
    public float cloudHeight=1f;
    public Mesh mMesh;
    public Material mMaterial;
    public bool mMatSetFloat = false;
    public float mMidYValue = 1f;
    public float mCloudHeight;
    public float mCloudClip;
    public int mLayer = 1;
    public Camera camera;
    public bool castShadows;
    public bool 一开始少了个维度 = true;
    private void Awake()
    {
        if(camera==null)
            camera = Camera.main;
    }

    void Update()
    {
        var position = transform.position;

        Vector3 startPos = position + Vector3.up * offset* stackCount / 2 ;
        for (int i = 0; i < stackCount; i++)
        {
            if(一开始少了个维度)
                DrawOnLayer(startPos,i);
            else
            {
                DrawByScale(startPos, i);    
            }
            
        }
    }

    void DrawOnLayer(Vector3 startPos,int curr)
    {
        
//        if (mMatSetFloat)
//        {
//            mMaterial.SetFloat("_midYValue", mMidYValue);
//            mMaterial.SetFloat("_cloudHeight", mCloudHeight);
//            mMaterial.SetFloat("_ClipGray", mCloudClip);
//        }

        var transform1 = transform;
        var matrix = Matrix4x4.TRS(startPos - offset * curr * Vector3.up, transform1.rotation, transform1.localScale);
        
        
        Graphics.DrawMesh(mMesh,matrix,mMaterial,mLayer);
        //Graphics.DrawMesh(mMesh, matrix, mMaterial, mLayer, Camera.main, 0, null, true, false, false);
    }

    void DrawByScale(Vector3 startPos,int i)
    {
        offset = cloudHeight / stackCount / 2f;
        mMaterial.SetFloat("_midYValue", transform.position.y);
        mMaterial.SetFloat("_cloudHeight", cloudHeight);
        var transform1 = transform;
        Vector3 startFinalPos = transform1.position + (Vector3.up * (offset * stackCount / 2f));
        var matrix = Matrix4x4.TRS(startFinalPos - (offset * i * Vector3.up), transform1.rotation, transform1.localScale);
        Graphics.DrawMesh(mMesh, matrix, mMaterial, mLayer, camera, 0, null, castShadows, false, false); // otherwise just draw it now
    }
}
